//
//  JHMainViewController.m
//  卡牌动画
//
//  Created by cjatech-JH on 16/1/14.
//  Copyright © 2016年 cjatech-JH. All rights reserved.
//

#import "JHMainViewController.h"
#define k_IOS_Scale [[UIScreen mainScreen] bounds].size.width/320
@interface JHMainViewController ()<UIScrollViewDelegate>{
    NSInteger _index;
    NSMutableArray *_imageArr;
    UIScrollView *_scrollView;
    BOOL _isRight;
}
@end

@implementation JHMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor darkGrayColor];
    _index = 0;
    _isRight = YES;
    [self configBaseImage];
    [self configBaseBtn];
}

- (void)configBaseImage{
    NSArray *arr = [[NSBundle mainBundle] pathsForResourcesOfType:@"jpg" inDirectory:[[NSBundle mainBundle] pathForResource:@"IMAGE" ofType:nil]];
    
    _imageArr = [NSMutableArray array];
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 100*k_IOS_Scale, 320*k_IOS_Scale, 280*k_IOS_Scale)];
    _scrollView.backgroundColor = [UIColor blackColor];
    _scrollView.contentSize = CGSizeMake((arr.count-1)*55*k_IOS_Scale+154*k_IOS_Scale+100*k_IOS_Scale, 0);
    _scrollView.delegate = self;
    [self.view addSubview:_scrollView];
    for (NSInteger i=0;i<arr.count;i++) {
        NSString *ss = [[arr[i] componentsSeparatedByString:@"/"] lastObject];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:ss]];
        imageView.tag = i;
        imageView.layer.borderWidth = 3;
        imageView.layer.cornerRadius = 10;
        imageView.clipsToBounds = YES;
        imageView.layer.borderColor = [UIColor yellowColor].CGColor;
        imageView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapClick:)];
        [imageView addGestureRecognizer:tap];
        if (i == 0) {
            imageView.frame = CGRectMake(0, 0, 154*k_IOS_Scale, 280*k_IOS_Scale);
            imageView.center = CGPointMake(160*k_IOS_Scale, 140*k_IOS_Scale);
        }else{
            UIImageView *ima = [_imageArr lastObject];
            if (i == 1) {
                imageView.frame = CGRectMake(CGRectGetMaxX(ima.frame)-20*k_IOS_Scale, 20*k_IOS_Scale, 154*k_IOS_Scale, 230*k_IOS_Scale);
            }else{
                imageView.frame = CGRectMake((ima.frame.origin.x)+15*k_IOS_Scale, 20*k_IOS_Scale, 144*k_IOS_Scale, 230*k_IOS_Scale);
            }
            CATransform3D t = CATransform3DIdentity;
            t.m34 = 4.5/-2000;
            t = CATransform3DRotate(t, M_PI/2*3/4, 0, 1, 0);
            imageView.layer.transform = t;
        }
        [_scrollView addSubview:imageView];
        [_imageArr addObject:imageView];
    }
}


- (void)imageTapClick:(UITapGestureRecognizer *)sender{
    
    if (_index == sender.view.tag) {
        return;
    }
    if (_index<sender.view.tag) {
        _isRight = YES;
    }else{
        _isRight = NO;
    }
    _index = sender.view.tag;
    [self updateFrameForVisilably];
    [self updateFrame];
}

- (void)updateFrame{
    for (NSInteger i = 0; i < _imageArr.count; i++) {
        UIImageView *imageV = _imageArr[i];
        if (i == _index) {
            if (i == 0) {
                imageV.frame = CGRectMake(0, 0, 154*k_IOS_Scale, 280*k_IOS_Scale);
                imageV.center = CGPointMake(160*k_IOS_Scale, 140*k_IOS_Scale);
            }else{
                UIImageView *ima = _imageArr[i-1];
                imageV.frame = CGRectMake(ima.frame.origin.x+80*k_IOS_Scale, 0, 154*k_IOS_Scale, 280*k_IOS_Scale);
            }
            if (_isRight) {
                CATransform3D t = CATransform3DIdentity;
                t.m34 = 4.5/-2000;
                t = CATransform3DRotate(t, 0, 0, 1, 0);
                imageV.layer.transform = t;
            }else{
                CATransform3D t = CATransform3DIdentity;
                t.m34 = 4.5/-2000;
                t = CATransform3DRotate(t, 0, 0, 1, 0);
                imageV.layer.transform = t;
            }
        }else if (i < _index){
            CATransform3D t = CATransform3DIdentity;
            t.m34 = 4.5/-2000;
            t = CATransform3DRotate(t, -M_PI/2*3/4, 0, 1, 0);
            imageV.layer.transform = t;
            imageV.frame = CGRectMake(55*i*k_IOS_Scale+30*k_IOS_Scale, 20*k_IOS_Scale, 144*k_IOS_Scale, 230*k_IOS_Scale);
            
        }else if (i > _index){
            CATransform3D t = CATransform3DIdentity;
            t.m34 = 4.5/-2000;
            t = CATransform3DRotate(t, M_PI/2*3/4, 0, 1, 0);
            imageV.layer.transform = t;
            imageV.frame = CGRectMake(55*(i-1)*k_IOS_Scale+30*k_IOS_Scale+154*k_IOS_Scale+55*k_IOS_Scale, 20*k_IOS_Scale, 144*k_IOS_Scale, 230*k_IOS_Scale);
        }
    }
    UIImageView *imaa = _imageArr[_index];
    _scrollView.contentOffset = CGPointMake(imaa.frame.origin.x-85, 0);
}

- (void)configBaseBtn{
    for (NSInteger i = 0; i < 2; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
        btn.frame = CGRectMake(10*k_IOS_Scale+280*i*k_IOS_Scale, 220*k_IOS_Scale, 20*k_IOS_Scale, 50*k_IOS_Scale);
        btn.backgroundColor = [UIColor yellowColor];
        btn.tag = i;
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn];
    }
}

- (void)btnClick:(UIButton *)sender{
    if (sender.tag == 0&&_index>0) {
        _index--;
        _isRight = NO;
        
    }else if(sender.tag == 1&&_index<_imageArr.count-1){
        _index++;
        _isRight = YES;
    }else{
        return;
    }
    [self updateFrameForVisilably];
    [self updateFrame];
}

- (void)updateFrameForVisilably{
    UIImageView *ima = _imageArr[_index];
    if (_index>0&&_index<_imageArr.count-1) {
        UIImageView *pre = _imageArr[_index-1];
        UIImageView *tail = _imageArr[_index+1];
        
        CATransform3D t = CATransform3DIdentity;
        t.m34 = 4.5/-2000;
        t = CATransform3DRotate(t, -M_PI/2*3/4, 0, 1, 0);
        
        CATransform3D t2 = CATransform3DIdentity;
        t2.m34 = 4.5/-2000;
        t2 = CATransform3DRotate(t2, 0, 0, 1, 0);
        
        CATransform3D t3 = CATransform3DIdentity;
        t3.m34 = 4.5/-2000;
        t3 = CATransform3DRotate(t3, M_PI/2*3/4, 0, 1, 0);
        
        UIImageView *four = nil;
        
        if (_index>2&&_index<_imageArr.count-2) {
            
            if (_isRight) {
                four = _imageArr[_index+2];
            }else{
                four = _imageArr[_index-2];
            }
        }
        
        [UIView animateWithDuration:0.5 animations:^{
            
            if (!_isRight) {
                four.frame = CGRectMake(55*(_index)*k_IOS_Scale+30*k_IOS_Scale+154*k_IOS_Scale+55*k_IOS_Scale, 20*k_IOS_Scale, 144*k_IOS_Scale, 230*k_IOS_Scale);
                
                tail.layer.transform = t3;
                tail.frame = CGRectMake(55*(_index-1)*k_IOS_Scale+30*k_IOS_Scale+154*k_IOS_Scale+55*k_IOS_Scale, 20*k_IOS_Scale, 144*k_IOS_Scale, 230*k_IOS_Scale);
                ima.layer.transform = t2;
                ima.frame = CGRectMake(55*(_index-1)*k_IOS_Scale+110*k_IOS_Scale, 0, 154*k_IOS_Scale, 280*k_IOS_Scale);
                pre.layer.transform = t;
                pre.frame = CGRectMake(55*(_index-1)*k_IOS_Scale+30*k_IOS_Scale, 20*k_IOS_Scale, 144*k_IOS_Scale, 230*k_IOS_Scale);
            }else{
                four.frame = CGRectMake(55*(_index-1)*k_IOS_Scale+30*k_IOS_Scale, 20*k_IOS_Scale, 144*k_IOS_Scale, 230*k_IOS_Scale);
                pre.layer.transform = t;
                pre.frame = CGRectMake(55*(_index-1)*k_IOS_Scale+30*k_IOS_Scale, 20*k_IOS_Scale, 144*k_IOS_Scale, 230*k_IOS_Scale);
                ima.layer.transform = t2;
                ima.frame = CGRectMake(55*(_index-1)*k_IOS_Scale+30*k_IOS_Scale+80*k_IOS_Scale, 0, 154*k_IOS_Scale, 280*k_IOS_Scale);
                tail.layer.transform = t3;
                tail.frame = CGRectMake(55*(_index-1)*k_IOS_Scale+30*k_IOS_Scale+154*k_IOS_Scale+55*k_IOS_Scale, 20*k_IOS_Scale, 144*k_IOS_Scale, 230*k_IOS_Scale);
            }
            
        } completion:^(BOOL finished) {
            //            [self updateFrame];
        }];
        
    }else if(_index == 0){
        
        UIImageView *tail = _imageArr[_index+1];
        CATransform3D t = CATransform3DIdentity;
        t.m34 = 4.5/-2000;
        t = CATransform3DRotate(t, -M_PI/2*3/4, 0, 1, 0);
        
        CATransform3D t2 = CATransform3DIdentity;
        t2.m34 = 4.5/-2000;
        t2 = CATransform3DRotate(t2, 0, 0, 1, 0);
        
        CATransform3D t3 = CATransform3DIdentity;
        t3.m34 = 4.5/-2000;
        t3 = CATransform3DRotate(t3, M_PI/2*3/4, 0, 1, 0);
        
        [UIView animateWithDuration:0.5 animations:^{
            ima.layer.transform = t2;
            ima.frame = CGRectMake(55*(_index-1)*k_IOS_Scale+110*k_IOS_Scale, 0, 154*k_IOS_Scale, 280*k_IOS_Scale);
            tail.layer.transform = t3;
            tail.frame = CGRectMake(55*(_index-1)*k_IOS_Scale+234*k_IOS_Scale, 20*k_IOS_Scale, 144*k_IOS_Scale, 230*k_IOS_Scale);
        }completion:^(BOOL finished) {
            //            [self updateFrame];
        }];
        
    }else if (_index == _imageArr.count-1){
        UIImageView *pre = _imageArr[_index-1];
        
        CATransform3D t = CATransform3DIdentity;
        t.m34 = 4.5/-2000;
        t = CATransform3DRotate(t, -M_PI/2*3/4, 0, 1, 0);
        
        CATransform3D t2 = CATransform3DIdentity;
        t2.m34 = 4.5/-2000;
        t2 = CATransform3DRotate(t2, 0, 0, 1, 0);
        [UIView animateWithDuration:0.5 animations:^{
            pre.layer.transform = t;
            pre.frame = CGRectMake(55*(_index-1)*k_IOS_Scale+30*k_IOS_Scale, 20*k_IOS_Scale, 144*k_IOS_Scale, 230*k_IOS_Scale);
            ima.layer.transform = t2;
            ima.frame = CGRectMake(55*(_index-1)*k_IOS_Scale+30*k_IOS_Scale+80*k_IOS_Scale, 0, 154*k_IOS_Scale, 280*k_IOS_Scale);
            
        }completion:^(BOOL finished) {
            //            [self updateFrame];
        }];
    }
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
}


- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    if (_index == _imageArr.count-1&&_scrollView.contentOffset.x<_scrollView.contentSize.width-80*k_IOS_Scale) {
        UIImageView *imaa = _imageArr[_index];
        _scrollView.contentOffset = CGPointMake(imaa.frame.origin.x-85*k_IOS_Scale, 0);
    }else if (_index == 0&&_scrollView.contentOffset.x<80*k_IOS_Scale){
        UIImageView *imaa = _imageArr[_index];
        _scrollView.contentOffset = CGPointMake(imaa.frame.origin.x-85*k_IOS_Scale, 0);
    }
}

@end
